package com.itfitness.opencvincludedemo;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {

    // Used to load the 'native-lib' library on application startup.
    static {
        System.loadLibrary("native-lib");
    }
    private ImageView imgIcon;
    private ImageView imgIcontwo;
    private ImageView imgIconthree;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        imgIcon = (ImageView) findViewById(R.id.img_icon);
        imgIcontwo = (ImageView) findViewById(R.id.img_icontwo);
        imgIconthree = (ImageView) findViewById(R.id.img_iconthree);
    }
    /**
     * 将Bitmap转换为灰度图
     */
    public native void converImage2Gray(Bitmap bitmap);

    /**
     * 裁剪银行卡
     * @param bitmap
     */
    public native Bitmap cropImage(Bitmap bitmap);
    public void onClick(View view) {
        Bitmap bitmap = null;
        switch (view.getId()){
            case R.id.bt_one:
                bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.card1);
                //裁剪后的银行卡图像
                Bitmap bitmapCardOne = cropImage(bitmap);
                imgIcon.setImageBitmap(bitmapCardOne);
                break;
            case R.id.bt_two:
                bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.card2);
                //裁剪后的银行卡图像
                Bitmap bitmapCardTwo = cropImage(bitmap);
                imgIcontwo.setImageBitmap(bitmapCardTwo);
                break;
            case R.id.bt_three:
                bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.card3);
                //裁剪后的银行卡图像
                Bitmap bitmapCardThree = cropImage(bitmap);
                imgIconthree.setImageBitmap(bitmapCardThree);
                break;
        }
        //释放源Bitmap
        if(bitmap != null){
            bitmap.recycle();
        }
    }
}
